import { _decorator, Component, Node, Label, Vec3 } from 'cc';
import { BasicUI } from '../../Init/Basic/BasicUI';
import { GlobalEnum } from '../../Init/Config/GlobalEnum';
import { GlobalTmpData } from '../../Init/Config/GlobalTmpData';
import { EventTypes } from '../../Init/Managers/EventTypes';
import { StorageSystem } from '../../Init/SystemStorage/StorageSystem';
import { UIEnum } from '../../Init/SystemUI/UIEnum';
import { UISystem } from '../../Init/SystemUI/UISystem';
import { clog } from '../../Init/Tools/ColorLog';
import { AwardBoxCfg } from '../LevelAwardBoxUI/LevelAwardBoxUI';
const { ccclass, property } = _decorator;

@ccclass('LevelInfoUI')
export class LevelInfoUI extends BasicUI {

    @property(Label)
    protected lvLabel: Label = null;

    @property(Node)
    protected lvInfo: Node = null;

    @property(Node)
    protected buffBtns: Node = null;

    private _timeScale = 1.0;
    private _isRun = false;

    public onEvents() {
        this.on(EventTypes.TouchEvents.TouchStart, this.onTouchStart, this);

    }

    public show(d?) {
        super.show(d);
        this.setLvInfo();
        this._isRun = false;
        this._timeScale = 1.0;

        this.lvInfo.active = false;
        this.buffBtns.active = false;
    }

    // #region ---------私有--------
    private setLvInfo() {
        this.lvLabel.string = StorageSystem.getData().levelAssets.curLv + '';
    }

    // #endregion

    // #region ----------事件
    protected onReadyTouch() {
    }

    protected onTouchStart() {
        if (this._isRun) return;
        this._isRun = true;
        // this.emit(EventTypes.GameEvents.GameRun);
        // clog.warn('GameRun');
        this.lvInfo.active = true;
        this.buffBtns.active = true;

        UISystem.hideUI(UIEnum.MapUI);
        //test
        // setTimeout(() => {
        //     StorageSystem.setData((d)=>{
        //         d.userAssets.keyNum=3;
        //     },true)
        //     UISystem.showUI(UIEnum.FinishAwardBoxUI, false);
        // }, 3000);

        // wx事件
        // wxsdk.GetAdById('GameStartClick');
    }

    // #endregion

    // #region ------------按钮---------
    protected onSpdUpClick() {
        this._timeScale += 0.5;
        this.emit(EventTypes.GameEvents.SetGameTimeScale, this._timeScale);
    }
    protected onSpdCutClick() {
        this._timeScale -= 0.5;
        this._timeScale = this._timeScale < 0 ? 0 : this._timeScale;
        this.emit(EventTypes.GameEvents.SetGameTimeScale, this._timeScale);
    }

    protected onAddZombieClick() {
        this.emit(EventTypes.GameEvents.GamePause);
        this.emit(EventTypes.SDKEvents.ShowVideo, {
            success: () => {
                //观看视频成功
                this.emit(EventTypes.RoleEvents.AddProtected, 10);
                // this.addBtn.active = false;
                this.emit(EventTypes.GameEvents.GameResume);
            },
            fail: () => {
                this.emit(EventTypes.GameEvents.GameResume);
            },
            cancel: () => {
                this.emit(EventTypes.GameEvents.GameResume);
            },
        })
    }
    //加速道具
    protected onAddSpdClick() {
        this.emit(EventTypes.GameEvents.GamePause);
        this.emit(EventTypes.SDKEvents.ShowVideo, {
            success: () => {
                //观看视频成功     
                // this.addBtn.active = false;
                this.emit(EventTypes.GameEvents.GameResume);

                //奖励
                let cfg = AwardBoxCfg[3];
                let award = cfg.video;

                this.emit(EventTypes.RoleEvents.AddBuff,
                    { type: GlobalEnum.BuffType.AddSpeed, num: award, delay: cfg.delay });
                //效果
                // let d = { t: GlobalEnum.Effect2DType.GetSpdUp, p: Vec3.ZERO, delay: cfg.delay, isOne: true };
                // this.emit(EventTypes.EffectEvents.showUIEffect, d);
            },
            fail: () => {
                this.emit(EventTypes.GameEvents.GameResume);
            },
            cancel: () => {
                this.emit(EventTypes.GameEvents.GameResume);
            },
        })

        // wx事件
        // wxsdk.GetAdById('UseSpdPropClick');
    }
    //
    protected testWin() {
        this.emit(EventTypes.GameEvents.GameOver, true);
    }

    protected testLose() {
        this.emit(EventTypes.GameEvents.GameOver, false);
    }

    protected testPause() {
        this.emit(EventTypes.GameEvents.GamePause);
    }

    protected testResume() {
        this.emit(EventTypes.GameEvents.GameResume);
    }

    // #endregion
}

